package com.myk.game.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.myk.game.lib.R;


public class StarView extends View {

    //注意！并非只是不显示，而是将分数概念改为了基数概念！这是本控件与其它相似控件的核心区别。
    private boolean starIsShowEmpty = true;//是否显示空心部分
    private int starTotal = 5;  //星星总数
    private int starNum = 0;   //星星数量
    private int starSize = dp2px(20f);   //星星尺寸
    private int starDistance = 0;   //星星间距
    private int starOrientation = Orientation.HORIZONTAL;    //控件方向
    private Drawable starEmptyDrawable; //空心图片
    private Drawable starFullDrawable;  //实心图片
    private Paint paint;

    public interface Orientation {
        int HORIZONTAL = 0;
        int VERTICAL = 1;
    }

    public int getStarTotal() {
        return starTotal;
    }

    public StarView setStarTotal(int starTotal) {
        this.starTotal = starTotal;
        requestLayout();
        return this;
    }

    public int getStarNum() {
        return starNum;
    }

    public StarView setStarNum(int starNum) {
        this.starNum = starNum;
        requestLayout();
        return this;
    }

    public int getStarSize() {
        return starSize;
    }

    public StarView setStarSize(int starSize) {
        this.starSize = starSize;
        requestLayout();
        return this;
    }

    public int getStarDistance() {
        return starDistance;
    }

    public StarView setStarDistance(int starDistance) {
        this.starDistance = starDistance;
        requestLayout();
        return this;
    }

    public int getStarOrientation() {
        return starOrientation;
    }

    public void setStarOrientation(@IntRange(from = 0, to = 1) int starOrientation) {
        if (starOrientation != 0 && starOrientation != 1) {
            return;
        }
        this.starOrientation = starOrientation;
        requestLayout();
    }

    public Drawable getStarEmptyDrawable() {
        return starEmptyDrawable;
    }

    public void setStarEmptyDrawable(Drawable starEmptyDrawable) {
        this.starEmptyDrawable = starEmptyDrawable;
        invalidate();
    }

    public Drawable getStarFullDrawable() {
        return starFullDrawable;
    }

    public void setStarFullDrawable(Drawable starFullDrawable) {
        this.starFullDrawable = starFullDrawable;
        invalidate();
    }

    public boolean isStarIsShowEmpty() {
        return starIsShowEmpty;
    }

    public void setStarIsShowEmpty(boolean starIsShowEmpty) {
        this.starIsShowEmpty = starIsShowEmpty;
        requestLayout();
    }

    private void init(@NonNull Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.StarView);
        starTotal = mTypedArray.getInteger(R.styleable.StarView_star_total, 5);
        starNum = mTypedArray.getInteger(R.styleable.StarView_star_num, 0);
        starSize = (int) mTypedArray.getDimension(R.styleable.StarView_star_size, dp2px(20f));
        starDistance = (int) mTypedArray.getDimension(R.styleable.StarView_star_distance, 0);
        starOrientation = mTypedArray.getInt(R.styleable.StarView_star_orientation, Orientation.HORIZONTAL);
        starEmptyDrawable = mTypedArray.getDrawable(R.styleable.StarView_star_empty_drawable);
        starFullDrawable = mTypedArray.getDrawable(R.styleable.StarView_star_full_drawable);
        starIsShowEmpty = mTypedArray.getBoolean(R.styleable.StarView_star_is_show_empty, true);
        mTypedArray.recycle();

        if (starIsShowEmpty == false) {
            //并非只是不显示，而是将分数概念改为了基数概念！
            starTotal = starSize;
        }

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setFilterBitmap(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        switch (starOrientation) {
            case Orientation.HORIZONTAL:
                setMeasuredDimension((starIsShowEmpty ? starTotal : starNum) * (starSize + starDistance) - starDistance, starSize);
                break;
            case Orientation.VERTICAL:
                setMeasuredDimension(starSize, (starIsShowEmpty ? starTotal : starNum) * (starSize + starDistance) - starDistance);
                break;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (starOrientation == Orientation.HORIZONTAL) {
            //先画横向的实心部分，从左往右
            for (int i = 0; i < starNum; i++) {
                int cursor = i * (starSize + starDistance);
                starFullDrawable.setBounds(cursor + 0, 0, cursor + starSize, starSize);
                starFullDrawable.draw(canvas);
            }
            if (starIsShowEmpty) {
                //画横向空心部分，从右往左
                for (int i = starTotal; i > starNum - 1; i--) {
                    int cursor = i * (starSize + starDistance);
                    starEmptyDrawable.setBounds(cursor + 0, 0, cursor + starSize, starSize);
                    starEmptyDrawable.draw(canvas);
                }
            }
        } else if (starOrientation == Orientation.VERTICAL) {
            //先画纵向的实心部分，从上往下
            for (int i = 0; i < starNum; i++) {
                int cursor = i * (starSize + starDistance);
                starFullDrawable.setBounds(0, cursor + 0, starSize, cursor + starSize);
                starFullDrawable.draw(canvas);
            }
            if (starIsShowEmpty) {
                //画纵向的空心部分，从下往上
                for (int i = starTotal; i > starNum - 1; i--) {
                    int cursor = i * (starSize + starDistance);
                    starEmptyDrawable.setBounds(0, cursor + 0, starSize, cursor + starSize);
                    starEmptyDrawable.draw(canvas);
                }
            }
        }
    }

    private int dp2px(float dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public StarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
}
